import java.util.*;
import java.awt.Desktop;
import java.io.*;
import java.net.*;

public class Webpage
{
  private int chapterNumber = 0;
  private Historie historie;
  private String html;
  private File file;
  private String newline;
  private String titleOfStory;
  
  public Webpage(String filename, String title)
  {
    titleOfStory = title;
    historie = new Historie();
    this.file = new File(filename);
    this.newline = System.getProperty("line.separator");
    this.html = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>" + newline + 
      "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"" + newline + "\t\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" + newline +
      "<html xmlns=\"http://www.w3.org/1999/xhtml\">" + newline;
    
    writeHead(title);
    writeBody();
    
    html += "</html>";
    
    writeToFile();
  }
  
    /** Method to add the historie, containing a number of Kapitels, which each contain a number of Saetnings, to the html string
      * @return a string containing the historie
    * */  
  
  private String historieToXHTMLString()
  {
    ArrayList<Kapitel> kapitlerArrayList = historie.getKapitler();
    String returnedString = "";
    returnedString += newline;
    returnedString += "<h2>";
    returnedString += titleOfStory;
    returnedString += "</h2>";
    
    for(int i = 0; i < historie.getAntalKapitler(); i++)
    {
      returnedString += kapitelToXHTMLString( kapitlerArrayList.get(i) );      
    }
    returnedString += newline;
    returnedString += "Og de levede lykkeligt til deres dages ende";

      
    return returnedString;
  }
  
    /** Method to add the Saetning part to the html string
      * @return a string containing the Kapitel
    * */
  
  
  private String kapitelToXHTMLString(Kapitel k)
  {
    chapterNumber++;
    String returnedString = "";
    ArrayList<Saetning> saetningArrayListTemp = k.getSaetninger();
    
    returnedString += newline + "\t";
    returnedString += "<h3>";
    returnedString += "Kapitel";
    returnedString += " " + String.valueOf(chapterNumber);
    returnedString += "</h3>";
    
    returnedString += k.toString();
    
    returnedString += newline + "\t";

    
    return returnedString;
  }
  
  /** Method to add the Saetning part to the html string
    * @return a string containing the Saetning
    * */
  
  private String saetningToXHTMLString(Saetning s)
  {
    String returnedString = "";
    
    returnedString += newline + "\t\t";   
    returnedString += "<h4>" + s.toString() + "</h4>";
      
    return returnedString;
  }
  
  /** Method to add the <head> and <title> part to the html string
    * */
  
  private void writeHead(String title)
  {
    html += "<head>" + newline +
      "\t<title>" + title + "</title>" + newline +
      "</head>" + newline + newline;
  }
  
  /** Method to add the <body> part to the html string
    * */
  
  private void writeBody()
  {
    html += "<body>" +
      historieToXHTMLString() + newline +
      "</body>" + newline;
  }
  
  /** Method to write the HTML string to a file
    * */
  
  private void writeToFile()
  {
     try 
     {
       BufferedWriter writer = new BufferedWriter(new FileWriter(file));
       writer.write(html);
       writer.close();  
     } 
     catch (Exception ex) 
     {
       ex.printStackTrace();
     }
  }
  
  public void showInBrowser()
  {
   try 
    {
      Desktop.getDesktop().browse(new URI(file.getName()));
    }
    catch (IOException e) 
    {
      e.printStackTrace();
    } 
    catch (URISyntaxException e) 
    {
      e.printStackTrace();
    } 
  }
  
  public void showInEditor()
  {
       try 
    {
      Desktop.getDesktop().edit(file);
    }
    catch (IOException e) 
    {
      e.printStackTrace();
    } 
  }
  
}